<template>
  <div class="user-center">
    <el-card class="user-info">
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
          <el-button type="primary" @click="handleEdit">编辑</el-button>
        </div>
      </template>
      <div class="info-content">
        <el-avatar :size="80" :src="userInfo.avatar" />
        <div class="info-list">
          <div class="info-item">
            <span class="label">用户名：</span>
            <span>{{ userInfo.username }}</span>
          </div>
          <div class="info-item">
            <span class="label">姓名：</span>
            <span>{{ userInfo.name }}</span>
          </div>
          <div class="info-item">
            <span class="label">性别：</span>
            <span>{{ userInfo.gender === 'male' ? '男' : '女' }}</span>
          </div>
          <div class="info-item">
            <span class="label">手机号：</span>
            <span>{{ userInfo.phone }}</span>
          </div>
          <div class="info-item">
            <span class="label">邮箱：</span>
            <span>{{ userInfo.email }}</span>
          </div>
        </div>
      </div>
    </el-card>

    <el-card class="appointment-history">
      <template #header>
        <div class="card-header">
          <span>预约记录</span>
        </div>
      </template>
      <!-- 预约记录列表 -->
    </el-card>

    <el-dialog
      v-model="dialogVisible"
      title="编辑个人信息"
      width="500px"
    >
      <el-form :model="editForm" :rules="rules" ref="editFormRef" label-width="80px">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="/api/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="editForm.avatar" :src="editForm.avatar" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="editForm.gender">
            <el-radio :value="'male'">男</el-radio>
            <el-radio :value="'female'">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const userInfo = reactive({
  username: 'user123',
  name: '张三',
  gender: 'male',
  phone: '13800138000',
  email: 'zhangsan@example.com',
  avatar: ''
})

const dialogVisible = ref(false)
const editFormRef = ref(null)
const editForm = reactive({...userInfo})

const rules = {
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

const handleEdit = () => {
  Object.assign(editForm, userInfo)
  dialogVisible.value = true
}

const handleAvatarSuccess = (response) => {
  editForm.avatar = response.url
}

const submitForm = async () => {
  if (!editFormRef.value) return

  try {
    await editFormRef.value.validate()
    // TODO: 调用更新API
    Object.assign(userInfo, editForm)
    ElMessage.success('个人信息更新成功')
    dialogVisible.value = false
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

<style scoped lang="scss">
.user-center {
  .user-info {
    margin-bottom: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .info-content {
      display: flex;
      gap: 40px;

      .info-list {
        flex: 1;

        .info-item {
          margin-bottom: 15px;
          display: flex;
          align-items: center;

          .label {
            width: 80px;
            color: #909399;
          }
        }
      }
    }
  }

  .avatar-uploader {
    :deep(.el-upload) {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;

      &:hover {
        border-color: var(--el-color-primary);
      }
    }
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
  }

  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
}
</style> 